'use strict';
let reg = /^\[CQ:at,qq=1544392599] 查询(.*?)$/

const Service = require('egg').Service;

class WaterService extends Service {
  async regular(res) {
    let str = res.message;
    // console.log(str)
    return reg.test(str);
  }

  async waterBal(res) {
    let sendMsg = this.ctx.service.sendMsg;
    let id = res.message.match(reg);
    // console.log(id); 
    id = id[1];
    // console.log(id);

    let bal = await this.ctx.curl(`https://3548f2718x.goho.co/water`, {
      data: {
        id: id
      },
      timeout: [5000, 30000],
    })
    bal = JSON.parse(bal.data.toString());
    console.log(bal)
    if (bal.state == 'OK') {
      sendMsg.sendToGroup({
        id: res.group_id,
        msg: `${bal.data.id}余额：${bal.data.balance}`
      })
    } else if (bal.state == 'ERROR') {
      sendMsg.sendToGroup({
        id: res.group_id,
        msg: `格式错误，请重新输入`
      })
    }
  }
}

module.exports = WaterService;